var BaseHandler     = require('../base-handler');
var SystemServices  = require('../../../constants').SystemServices;
var HTTP_VERBS      = require('../../../constants').HTTP_VERBS;

class CreateflowHandler extends BaseHandler {
    get pattern() {
        return "/leadflows/:leadflow_id/edit";
    }

    doGET(request, response) {
        if (!request.session.user.authenticated) {
            response.redirect('/login');
        } else {
            //response.render('createflow');
            var leadflowId = request.params.leadflow_id;
            this.Controller.getLeadFlowMetadataById(leadflowId, (result) => {
                if (!result.success) {
                    response.render('errors/system-error');
                } else {
                    response.render('createflow', {
                        user: request.session.user,
                        metadata: result.data
                    });
                }
            });
        }
    }

    doPOST(request, response) {
        if (!request.session.user.authenticated) {
            response.json({
                success: false,
                error: '请登录系统后在进行保存'
            });
        } else {
            var leadflowId = request.params.leadflow_id;
            var metadata = request.body;
            this.Controller.updateLeadFlowMetadataById(leadflowId, metadata, (result) => {
                response.json(result);
            });
        }
    }

    setup() {
        this.registerVerb(HTTP_VERBS.POST, this.doPOST);
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
    }
}

module.exports = CreateflowHandler;